// pages/login/login.js
var app = getApp();
const fetch = require('../../utils/fetch') 
import Cache from "../../utils/cache";
import Tool from "../../utils/tool";
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },
  backPage: function () {
    wx.navigateBack({});
  },
  getLoginUser: function () {
    wx.showLoading({
      title: "正在登录...",
      mask: true
    })
    wx.getUserProfile({
      desc: "获取用户相关信息",
      success: res => {
        if (res.errMsg === "getUserProfile:ok") {
          let username = res.userInfo.nickName;
          let headPic = res.userInfo.avatarUrl;
          wx.login({
            success: async res => {
              console.log(res)
              if (res.errMsg === "login:ok") {
                // 调用后端接口，验证用户数据
                var data = {
                  loginurl: 'https://api.weixin.qq.com/sns/jscode2session',
                  appid: 'wx03f531e553c78333', 
                  wxHeadPic: headPic,
                  wxUsername: username,
                  code: res.code
                };
                fetch(`/login`, data, "post")
                  .then(response => {
                    console.log(response);
                    console.log(response.data.code === 200);
                    if (response.data.code === 200) {
                      response.data.msg, 
                      wx.showToast({
                        icon: "error",
                        title:  response.data.msg,
                        duration: 1000
                      });  
                      Cache.setCache(app.globalData.WXID, response.data.data.wxid);
                      Cache.setCache(app.globalData.HEADPIC, response.data.data.headpic);
                      Cache.setCache(app.globalData.USERTYPE, response.data.data.usertype);
                      Cache.setCache(app.globalData.STARTTIME, response.data.data.starttime);
                      Cache.setCache(app.globalData.ENDTIME, response.data.data.endtime);
                      Cache.setCache(app.globalData.USERNAME, response.data.data.username);
                      app.userInfo.isLogin = true;
                      app.userInfo.wxid = response.data.data.wxid;
                      app.userInfo.headpic = response.data.data.headpic; 
                      app.userInfo.usertype = response.data.data.usertype;
                      app.userInfo.starttime = response.data.data.starttime;
                      app.userInfo.endtime = response.data.data.endtime;
                      app.userInfo.username = response.data.data.username;
                      //关闭当前页
                      wx.navigateBack({});
                    } else {
                      wx.showToast({
                        icon: "error",
                        title:  response.data.msg,
                        duration: 1000
                      }); 
                    }
                  }).catch(response => {
                    wx.showToast({
                      icon: "error",
                      title: "登录失败"
                    });
                  })

              } else {
                wx.showToast({
                  icon: "error",
                  title: "登录失败"
                });
                wx.hideLoading();
              }

            },
            fail: res => {
              wx.showToast({
                icon: "error",
                title: "登录失败"
              });
              wx.hideLoading();
            }
          })
        } else {
          wx.showToast({
            icon: "error",
            title: "获取用户失败"
          });
          wx.hideLoading();
        }
      },
      fail: res => {
        console.log(res)
        wx.showToast({
          icon: "error",
          title: "获取用户失败"
        }); 
      }
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})